package org.dromara.demo.designPattern.责任链模式.service.impl;

import org.dromara.demo.designPattern.责任链模式.controller.AuthLink;
import org.dromara.demo.designPattern.责任链模式.entity.AuthInfo;
import org.dromara.demo.designPattern.责任链模式.service.AuthService;

import java.text.ParseException;
import java.util.Date;

public class Level1AuthLink extends AuthLink {


    private Date beginDate = f.parse("2020-06-11 00:00:00");
    private Date endDate = f.parse("2020-06-20 23:59:59");

    public Level1AuthLink(String levelUserId, String levelUserName) throws ParseException {
        super(levelUserId, levelUserName);
    }
    @Override
    public AuthInfo doAuth(String uId, String orderId, Date authDate) {
        Date date = AuthService.queryAuthInfo(levelUserId, orderId);
        /**
         * 没有审批信息
         */
        if (null == date) {
            return new AuthInfo("0001", "单号：", orderId, "状态：待一级审批负责人 ", levelUserName);
        }
        /**
         * 审批信息存在，判断是否需要下个节点审批
         */
        AuthLink next = super.next();
        //不需要下个节点审批，直接返回结果
        if (null == next) {
            return new AuthInfo("0000", "单号：", orderId, "状态：三级审批负责人完成", " 时间：", f.format(date), " 审批人：",
                    levelUserName);
        }
        //需要下个节点审批，判断时间是否在时间段内
        if (authDate.before(beginDate) || authDate.after(endDate)) {
            //不在时间段内，直接返回结果
            return new AuthInfo("0000", "单号：", orderId, "状态：三级审批负责人完成", " 时间：", f.format(date), " 审批人：", levelUserName);
        }
        //在时间段内，继续下一个节点审批
        return next.doAuth(uId, orderId, authDate);
    }

}
